const gulp = require('gulp')
const cssmin = require('gulp-cssmin')
const sass = require('gulp-sass')(require('sass'))
const uglify = require('gulp-uglify')
const babel = require('gulp-babel')
const htmlmin = require('gulp-htmlmin')
const clean = require('gulp-clean')
const webserver = require('gulp-webserver')
gulp.task('css', function() {
    return gulp
        .src('src/CSS/index.css')
        .pipe(cssmin())
        .pipe(gulp.dest('dist/css'))

})
gulp.task('sass', function() {
    return gulp
        .src('src/sass/**')
        .pipe(sass())
        .pipe(cssmin())
        .pipe(gulp.dest('dist/css'))
})
gulp.task('js', function() {
    return gulp
        .src('src/JS/jjss.js')
        .pipe(uglify())
        .pipe(gulp.dest('dist/js'))
})
gulp.task('syh', function() {
    return gulp
        .src('src/JS/babel.js')
        .pipe(babel({
            presets: ['es2015']
        }))
        // .pipe(uglify())
        .pipe(gulp.dest('dist/js/'))
})
gulp.task('html', function() {
    return gulp
        .src('src/pages/**')
        .pipe(htmlmin({
            removeEmptyAttributes: true,
            collapseWhitespace: true,
        }))
        .pipe(gulp.dest('dist/pages'))
})
gulp.task('lib', function() {
    return gulp
        .src('src/lib/**')
        .pipe(gulp.dest('dist/lib/'))
})
gulp.task('static', function() {
    return gulp
        .src('src/static/**')
        .pipe(gulp.dest('dist/static/'))
})

gulp.task('clean', function() {
    return gulp
        .src('dist/')
        .pipe(clean())
})
gulp.task('webserver', function() {
    return gulp
        .src('dist')
        .pipe(webserver({
            host: 'localhost',
            port: 3000,
            livereload: true,
            open: 'pages/htmlmin.html'
        }))
})

gulp.task('watch', function() {
    gulp.watch('src/css/**'), gulp.series('css')
    gulp.watch('src/sass/**'), gulp.series('sass')
    gulp.watch('src/js/**'), gulp.series('js')
    gulp.watch('src/html/**'), gulp.series('html')
    gulp.watch('src/lib/**'), gulp.series('lib')
    gulp.watch('src/static/**'), gulp.series('static')
})

// gulp.task('mytask', gulp.series('css', 'sass', 'js', 'syh', 'html', 'lib', 'static'))
// exports.default = gulp.series('css', 'sass', 'js', 'html', 'lib', 'static', 'webserver', 'watch')

// exports.default = gulp.parallel('css', 'sass', 'js', 'syh', 'html', 'lib', 'static')
// exports.default = gulp.series('clean', gulp.series('css', 'sass', 'js', 'html', 'lib', 'static'))
// exports.default = gulp.series('clean', gulp.parallel('css', 'sass', 'js', 'html', 'lib', 'static'), gulp.series('webserver', 'watch'))
exports.default = gulp.series('clean', gulp.parallel('css', 'sass', 'js', 'html', 'lib', 'static'), gulp.series('webserver', 'watch'))